package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.PriceComboQuotation;
import com.sz.biz.logistics.core.service.PriceComboQuotationService;
import com.sz.biz.logistics.core.dto.PriceComboQuotationDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: com.sz.biz.logistics.core.service.impl <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-04-18 16:49:00.
 */
@Service
public class PriceComboQuotationServiceImpl extends AbstractService implements PriceComboQuotationService {
    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.logistics.core.PriceComboQuotationMapper";
    }

    @Override
    public Integer save(PriceComboQuotation comboQuotation) {
        dao.save(getSqlName("insertSelective"), comboQuotation);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRICE_COMBO_QUOTATION", comboQuotation);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.ADD, "添加价格套餐关系", dataBackup);
        return comboQuotation.getId();
    }

    @Override
    public void batchSave(List<PriceComboQuotation> entry) {
        dao.batchInsert(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRICE_COMBO_QUOTATION", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.ADD, "添加价格套餐关系", dataBackup);
    }

    @Override
    public void delete(Integer id) {
        dao.delete(getSqlName("deleteByPrimaryKey"), id);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_PRICE_COMBO_QUOTATION", id);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.DELETE, "删除价格套餐关系", dataBackup);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        dao.batchDelete(getSqlName("deleteByPrimaryKey"), ids);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_PRICE_COMBO_QUOTATION", ids);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.DELETE, "删除价格套餐关系", dataBackup);
    }

    @Override
    public void update(PriceComboQuotation comboQuotation) {
        dao.update(getSqlName("updateByPrimaryKeySelective"), comboQuotation);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_PRICE_COMBO_QUOTATION", comboQuotation);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.UPDATE, "修改价格套餐关系", dataBackup);
    }

    @Override
    public void batchUpdate(List<PriceComboQuotation> comboQuotationList) {
        dao.batchUpdate(getSqlName("updateByPrimaryKeySelective"), comboQuotationList);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_PRICE_COMBO_QUOTATION", comboQuotationList);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_PRICE_COMBO_QUOTATION, UserActions.UPDATE, "修改价格套餐关系", dataBackup);
    }

    @Override
    public PriceComboQuotation findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        PriceComboQuotation entry = (PriceComboQuotation) dao.findForObject(getSqlName("selectByPrimaryKey"), pd);
        return entry;
    }

    @Override
    public List<PriceComboQuotation> findByIds(List<Integer> ids) {
        List<PriceComboQuotation> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(ids)) {
            ParamData pd = new ParamData();
            pd.put("ids", ids);
            list = dao.findForList(getSqlName("findByIds"), pd, PriceComboQuotation.class);
        }
        return list;
    }

    @Override
    public List<PriceComboQuotation> findByComboId(Integer comboId) {
        ParamData pd = new ParamData();
        pd.put("comboId", comboId);
        List<PriceComboQuotation> list = dao.findForList(getSqlName("findByComboId"), pd, PriceComboQuotation.class);
        return list;
    }

    @Override
    public List<PriceComboQuotation> findByComboIds(List<Integer> ids) {
        List<PriceComboQuotation> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(ids)) {
            ParamData pd = new ParamData();
            pd.put("ids", ids);
            list = dao.findForList(getSqlName("findByComboIds"), pd, PriceComboQuotation.class);
        }
        return list;
    }

    @Override

    public Map<Integer, List<PriceComboQuotation>> findMapByComboIds(List<Integer> ids) {
        List<PriceComboQuotation> comboQuotationList = this.findByComboIds(ids);
        Map<Integer, List<PriceComboQuotation>> comboQuotationMap = new HashMap<>();
        for (PriceComboQuotation comboQuotation : comboQuotationList) {
            if (comboQuotationMap.get(comboQuotation.getComboId()) == null) {
                List<PriceComboQuotation> comboQuotations = new ArrayList<>();
                comboQuotationMap.put(comboQuotation.getComboId(), comboQuotations);
            }
            comboQuotationMap.get(comboQuotation.getComboId()).add(comboQuotation);
        }
        return comboQuotationMap;
    }

    @Override
    public Map<Integer, List<PriceComboQuotationDto>> findDtoMapByComboIds(List<Integer> ids) {
        List<PriceComboQuotation> comboQuotationList = this.findByComboIds(ids);

        Map<Integer, List<PriceComboQuotationDto>> comboQuotationDtoMap = new HashMap<>();
        for (PriceComboQuotation comboQuotation : comboQuotationList) {
            if (comboQuotationDtoMap.get(comboQuotation.getComboId()) == null) {
                List<PriceComboQuotationDto> comboQuotationDtoList = new ArrayList<>();
                comboQuotationDtoMap.put(comboQuotation.getComboId(), comboQuotationDtoList);
            }
            comboQuotationDtoMap.get(comboQuotation.getComboId()).add(new PriceComboQuotationDto(comboQuotation));
        }
        return comboQuotationDtoMap;
    }


    @Override
    public List<PriceComboQuotation> findPriceQuotation(String code) {
        List<PriceComboQuotation> comboQuotationList = null;
        if (StringUtils.hasLength(code)) {
            ParamData pd = new ParamData();
            pd.put("code", code);
            comboQuotationList = dao.findForList(getSqlName("findPriceQuotation"), pd, PriceComboQuotation.class);
        }
        return comboQuotationList;
    }
}
